define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wddj/operationt/starting_price/index' + location.search,
                    add_url: 'wddj/operationt/starting_price/add',
                    edit_url: 'wddj/operationt/starting_price/edit',
                    del_url: 'wddj/operationt/starting_price/del',
                    multi_url: 'wddj/operationt/starting_price/multi',
                    import_url: 'wddj/operationt/starting_price/import',
                    table: 'wddj_starting_price_trailer',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'start_time', title: __('Start_time')},
                        {field: 'end_time', title: __('End_time')},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'vehicle_type', title: __('Vehicle_type'), operate:'LIKE'},
                        // {field: 'kilometer', title: __('Kilometer'), operate:'BETWEEN'},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $(function () {
                // 三级联动事件
                $('.province').on('change', function () {
                    var $this = $(this);
                    var pid = $this.val();
                    var $city = $this.closest('.fieldlist-item').find('.city');

                    if (!pid) {
                        $city.val('').prop('disabled', true);
                        return;
                    }

                    $.ajax({
                        url: 'ajax/areaMulite',
                        data: {pid: pid, level: 2},
                        success: function (res) {
                            $city.html('<option value="">选择城市</option>');
                            $.each(res.list, function (i, item) {
                                $city.append($('<option>', {
                                    value: item.id,
                                    text: item.name
                                }));
                            });
                            $city.prop('disabled', false);
                        }
                    });
                });

                $('.city').on('change', function () {
                    var $this = $(this);
                    var pid = $this.val();
                    var $district = $this.closest('.fieldlist-item').find('.district');

                    if (!pid) {
                        $district.val('').prop('disabled', true);
                        return;
                    }

                    $.ajax({
                        url: 'ajax/areaMulite',
                        data: {pid: pid, level: 3},
                        success: function (res) {
                            $district.html('<option value="">选择区县</option>');
                            $.each(res.list, function (i, item) {
                                $district.append($('<option>', {
                                    value: item.id,
                                    text: item.name
                                }));
                            });
                            $district.prop('disabled', false);
                        }
                    });
                });

                // 添加区域
                $('.add-area').click(function () {
                    var $item = $(this).closest('.fieldlist-item');
                    var districtId = $item.find('.district').val();
                    var fullName = [
                        $item.find('.province option:selected').text(),
                        $item.find('.city option:selected').text(),
                        $item.find('.district option:selected').text()
                    ].join('/');

                    if (!districtId) return;

                    // 防止重复添加
                    if ($('#selected-areas input[value="' + districtId + '"]').length > 0) {
                        return Toastr.warning('该区域已存在');
                    }

                    var $newItem = $('<div class="selected-item">')
                        .append('<span>' + fullName + '</span>')
                        .append('<input type="hidden" name="row[area_ids][]" value="' + districtId + '">')
                        .append('<a href="javascript:;" class="btn btn-danger btn-xs remove-item"><i class="fa fa-times"></i></a>');

                    $('#selected-areas').append($newItem);
                });

                // 删除区域
                $(document).on('click', '.remove-item', function () {
                    $(this).closest('.selected-item')
                        .animate({opacity: 0, marginLeft: '20px'}, 200, function () {
                            $(this).remove();
                        });
                });
            });

            Controller.api.bindevent();
        },
        edit: function () {
            $(function () {
                // 三级联动事件
                $('.province').on('change', function () {
                    var $this = $(this);
                    var pid = $this.val();
                    var $city = $this.closest('.fieldlist-item').find('.city');

                    if (!pid) {
                        $city.val('').prop('disabled', true);
                        return;
                    }

                    $.ajax({
                        url: 'ajax/areaMulite',
                        data: {pid: pid, level: 2},
                        success: function (res) {
                            $city.html('<option value="">选择城市</option>');
                            $.each(res.list, function (i, item) {
                                $city.append($('<option>', {
                                    value: item.id,
                                    text: item.name
                                }));
                            });
                            $city.prop('disabled', false);
                        }
                    });
                });

                $('.city').on('change', function () {
                    var $this = $(this);
                    var pid = $this.val();
                    var $district = $this.closest('.fieldlist-item').find('.district');

                    if (!pid) {
                        $district.val('').prop('disabled', true);
                        return;
                    }

                    $.ajax({
                        url: 'ajax/areaMulite',
                        data: {pid: pid, level: 3},
                        success: function (res) {
                            $district.html('<option value="">选择区县</option>');
                            $.each(res.list, function (i, item) {
                                $district.append($('<option>', {
                                    value: item.id,
                                    text: item.name
                                }));
                            });
                            $district.prop('disabled', false);
                        }
                    });
                });

                // 添加区域
                $('.add-area').click(function () {
                    var $item = $(this).closest('.fieldlist-item');
                    var districtId = $item.find('.district').val();
                    var fullName = [
                        $item.find('.province option:selected').text(),
                        $item.find('.city option:selected').text(),
                        $item.find('.district option:selected').text()
                    ].join('/');

                    if (!districtId) return;

                    // 防止重复添加
                    if ($('#selected-areas input[value="' + districtId + '"]').length > 0) {
                        return Toastr.warning('该区域已存在');
                    }

                    var $newItem = $('<div class="selected-item">')
                        .append('<span>' + fullName + '</span>')
                        .append('<input type="hidden" name="row[area_ids][]" value="' + districtId + '">')
                        .append('<a href="javascript:;" class="btn btn-danger btn-xs remove-item"><i class="fa fa-times"></i></a>');

                    $('#selected-areas').append($newItem);
                });

                // 删除区域
                $(document).on('click', '.remove-item', function () {
                    $(this).closest('.selected-item')
                        .animate({opacity: 0, marginLeft: '20px'}, 200, function () {
                            $(this).remove();
                        });
                });
            });

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
